﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SportStore.DomainModel.Abstract;
using SportStore.DomainModel.Concrete;
using SportStore.DomainModel.Entities;


namespace SportStore.WebUI.Controllers
{
    public class ProductsController : Controller
    {
        private const int PAGE_SIZE = 3;

        private IProductsRepository productRepository;
        public ProductsController(IProductsRepository productsRepository)
        {
            this.productRepository = productsRepository;
            //productRepository = new FakeProductsRepository();
            //string connectionString = @"Server=DOHOANGAN;Database=SportStore;User ID=sa;PWD=123456";
            //productRepository = new SqlProductsRepository(connectionString);
        }

        public ViewResult List(string category, int page)
        {
            var productInCategory = (category == null)
                                        ? productRepository.Products
                                        : productRepository.Products.Where(x => x.Category == category);

            int numProducts = productRepository.Products.Count();
            ViewData["TotalPages"] = (int)Math.Ceiling((double)numProducts / PAGE_SIZE);
            ViewData["CurrentPage"] = page;
            ViewData["CurrentCategory"] = category;

            return View(productInCategory.Skip((page - 1) * PAGE_SIZE).Take(PAGE_SIZE).ToList());
        }

        public  FileContentResult GetImage(int productID)
        {
            Product product = (from p in productRepository.Products where p.ProductID == productID select p).First();
            return File(product.ImageData, product.ImageMimeType);
        }
    }
}
